package com.mall507.fivepeaks.activity.find;

import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.main.MainOriginalRFM;

import java.util.ArrayList;
import java.util.List;

/**
 * 失物招领
 */
@Route(path = "/find/FindLostFoundUI")
public class FindLostFoundUI extends BaseUI {

    @ViewInject(R.id.tablayout)
    TabLayout mTabLayout;
    @ViewInject(R.id.viewPager)
    ViewPager mViewPager;

    String[] stateName = new String[]{"全部", "失物招领", "寻物启事", "寻人启事", "已认领"};
    int[] stateValue = new int[]{0, 1, 2, 3, 4};
    MyPagerAdapter myPagerAdapter;

    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_find_lost_found);
        getTitleView().setContent("失物招领");
        initViewPager();
        mViewPager.setOffscreenPageLimit(stateName.length);
    }
    
    private void initViewPager() {
        // 创建一个集合,装填Fragment
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(FindLostFoundRFM.newInstance(stateValue[0]));
        fragments.add(FindLostFoundRFM.newInstance(stateValue[1]));
        fragments.add(FindLostFoundRFM.newInstance(stateValue[2]));
        fragments.add(FindLostFoundRFM.newInstance(stateValue[3]));
        fragments.add(FindLostFoundRFM.newInstance(stateValue[4]));
        // 创建ViewPager适配器
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        myPagerAdapter.setFragments(fragments);
        // 给ViewPager设置适配器
        mViewPager.setAdapter(myPagerAdapter);
        // TabLayout 指示器 (记得自己手动创建4个Fragment,注意是 app包下的Fragment 还是 V4包下的 Fragment)
        for (int i = 0; i < stateValue.length; i++) {
            mTabLayout.addTab(mTabLayout.newTab());
        }
        // 使用 TabLayout 和 ViewPager 相关联
        mTabLayout.setupWithViewPager(mViewPager);
        // TabLayout指示器添加文本
        for (int i = 0; i < stateName.length; i++) {
            mTabLayout.getTabAt(i).setText(stateName[i]);
        }
        //tab的字体选择器,默认黑色,选择时红色
        mTabLayout.setTabTextColors(ContextCompat.getColor(this, R.color.base_666666), ContextCompat.getColor(this, R.color.application_color));
        //tab的下划线颜色,默认是粉红色
        mTabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.application_color));
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        //当前Fragment
        private List<Fragment> mFragmentList;

        @Override
        //每次Fragment切换都会调用到FragmentPagerAdapter.setPrimaryItem 方法
        public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            super.setPrimaryItem(container, position, object);
        }

        public void setFragments(ArrayList<Fragment> fragments) {
            mFragmentList = fragments;
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = mFragmentList.get(position);

            return fragment;
        }

        @Override
        public int getCount() {

            return mFragmentList.size();
        }
    }

}
